package com.fanketly.funacg2.ui.screen

import android.util.Log
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.paging.LoadState
import androidx.paging.compose.LazyPagingItems
import androidx.paging.compose.collectAsLazyPagingItems
import androidx.paging.compose.itemKey
import com.fanketly.funacg2.data.entity.HistoryEntity
import com.fanketly.funacg2.data.entity.conventToWork
import com.fanketly.funacg2.ui.components.SwipeToDelete
import com.fanketly.funacg2.ui.theme.FunACG2Theme
import com.fanketly.funacg2.ui.viewmodels.HistoryViewModel
import com.fanketly.funacg2.ui.viewmodels.HomeViewModel
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.navigation.DestinationsNavigator

@Destination()
@Composable
fun HistoryScreen(
    homeViewModel: HomeViewModel,
    historyViewModel: HistoryViewModel = hiltViewModel(),
    navigator: DestinationsNavigator,
) {
    val data = historyViewModel.pager.collectAsLazyPagingItems()
    Box(modifier = Modifier.fillMaxSize()) {
        HistoryContent(data, dismissedToEnd = homeViewModel::openDrawer, onDelete = {
            historyViewModel.delete(it)
        }, onClick = {
            homeViewModel.getAudio(it.conventToWork())
        })
    }
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun HistoryContent(
    lazyPagingItems: LazyPagingItems<HistoryEntity>,
    dismissedToEnd: () -> Unit,
    onDelete: (Int) -> Unit,
    onClick: (HistoryEntity) -> Unit,
) {
    if (lazyPagingItems.loadState.refresh == LoadState.Loading) {
        LinearProgressIndicator(
            modifier = Modifier.fillMaxWidth()
        )
    } else LazyColumn(
        modifier = Modifier
            .fillMaxSize()
            .padding(top = 8.dp),
    ) {
        items(lazyPagingItems.itemCount, key = lazyPagingItems.itemKey { it.id }) {
            val historyEntity = lazyPagingItems[it]
            if (historyEntity != null) SwipeToDelete(
                Modifier.animateItemPlacement(),
                title = historyEntity.title,
                thumbnailCoverUrl = historyEntity.thumbnailCoverUrl,
                dismissedToEnd = dismissedToEnd,
                onDelete = { onDelete(historyEntity.id) },
                onClick = { onClick(historyEntity) })
        }
    }
}

@Preview
@Composable
private fun HistoryPreview() {
    FunACG2Theme(true) {
        Surface(Modifier.fillMaxSize()) {
            LinearProgressIndicator(
                modifier = Modifier.fillMaxWidth()
            )
        }
    }
}